﻿
namespace CommonHelpers
{
    using System.Drawing;

    public static class Extensions
    {
        public static RectangleF MeasureDisplayString(this Graphics graphics, string text, Font font)
        {
            StringFormat format = new StringFormat();
            RectangleF rect = new RectangleF(0, 0, 1000, 1000);
            CharacterRange[] ranges =
                {
                    new CharacterRange(0, text.Length)
                };
            format.SetMeasurableCharacterRanges(ranges);

            Region[] regions = graphics.MeasureCharacterRanges(text, font, rect, format);
            rect = regions[0].GetBounds(graphics);
            return new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
        }
    }
}
